package com.thinkersre.controller.interceptor;

import com.thinkersre.entity.User;
import com.thinkersre.service.DataService;
import com.thinkersre.util.HostHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class DataInterceptor implements HandlerInterceptor {
    @Resource
    private DataService dataService;
    @Resource
    private HostHolder hostHolder;

    //先确定访问了，添加访问数据
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //统计访问量UV
        String ip = request.getRemoteHost();
        dataService.recordUV(ip);
        //统计用户访问量DAU
        User user = hostHolder.getUser();
        if(user!=null){
            dataService.recordDAU(user.getId());
        }
        return true;
    }
}
